package com.zhongshan.ga.mobile.logic;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.ibatis.session.SqlSession;
import org.json.JSONObject;
import org.restlet.representation.Representation;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhongshan.ga.commonvar.ArgsConfig;
import com.zhongshan.ga.commonvar.CommendConfig;
import com.zhongshan.ga.commonvar.PHPWebUrl;
import com.zhongshan.ga.logger.GenerateLog;
import com.zhongshan.ga.mybatis.bean.ActivityInfo;
import com.zhongshan.ga.mybatis.bean.ScenicCommentImg;
import com.zhongshan.ga.mybatis.bean.ScenicSpotsInfo;
import com.zhongshan.ga.mybatis.bean.SecnicConmonImg;
import com.zhongshan.ga.mybatis.dao.IScenicSpotsInfoDao;
import com.zhongshan.ga.utils.MyBatisUtil;

/**
 * 客户端请求景点信息列表的逻辑处理类
 * @author yanqi
 *
 */
public class GetScenicSpotsListInfoLogic extends ParentLogic {
	/**
	 * 用于处理客户端请求获取景点列表信息作品信息
	 * @param representation 客户端请求体
	 * @return JsonObject 
	 */
	public JsonObject getScenicSpotsList(Representation representation) {
		JsonObject acceptJsonObject;
		JsonObject resultJson = null ;
		Gson gson = new Gson();
		try {
			/**
			 * 获取客户客户端请求的内容
			 */
			// acceptJsonObject = new JsonParser().parse(gson.toJson(representation.getText()).getAsJsonObject();
			 String acceptString = representation.getText();
			// JSONObject new JSONObject(x)
			GenerateLog.outDebug(acceptString);
			acceptJsonObject = new JsonParser().parse(acceptString).getAsJsonObject();
			GenerateLog.outDebug(acceptJsonObject);
			/**
			 * 进行基本的逻辑较验
			 */
			resultJson = super.basicCheck(acceptJsonObject, CommendConfig.SCENIC_SPOTS_LIST, false);
			/**
			 * 判断是否逻辑较验正确
			 */
			if(resultJson.get(ArgsConfig.RESULT_CODE).getAsString().equals(ArgsConfig.RESPONSE_SUCCESS)) {
				GenerateLog.outDebug("进来了");
				// 获得请求的数据体
				 JsonObject dataJsonObject = acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(dataJsonObject);
				// 获得请求的记录下标
				int startIndex = dataJsonObject.get(ArgsConfig.START_INDEX).getAsInt();
				int endIndex = dataJsonObject.get(ArgsConfig.RECORD_NUM).getAsInt();
				String townshipCode = dataJsonObject.get(ArgsConfig.TOWNSHIP_CODE).getAsString();
				
				// 获得sqlSession对象
				SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
				// 获得景点列表Dao层实例
				IScenicSpotsInfoDao scenicSpotsInfoDao = sqlSession.getMapper(IScenicSpotsInfoDao.class);
				// 调用dao层获得景点列表信息
				ArrayList<ScenicSpotsInfo> scenicSpotsInfos = scenicSpotsInfoDao.getScenicSpotsInfoList(townshipCode, startIndex, endIndex);
				// 给图片加上图片路径
				if (scenicSpotsInfos != null) {
					for (ScenicSpotsInfo item : scenicSpotsInfos) {
						item.setScenicSpotsCover(PHPWebUrl.SCENIC_SPOTS_COVER_IMAGES_URL
								+ item.getScenicSpotsCover());
						item.setVoiceURL(PHPWebUrl.VOICE_URL + item.getVoiceURL());
					}
				}
				// 临时添加
				for(int i = 0;  i < scenicSpotsInfos.size(); i++) {
					ScenicSpotsInfo info = scenicSpotsInfos.get(i);
					
					ArrayList<SecnicConmonImg> scenicSpotsImgBeanArray = scenicSpotsInfoDao.getScenicSpotsInfoImgsList(info.getScenicSpotsCode());
					ArrayList<String> scenicSpotsImgsArray = new ArrayList<String>();
					for (SecnicConmonImg scenicCommentImg : scenicSpotsImgBeanArray) {
						scenicSpotsImgsArray.add(PHPWebUrl.SCENIC_SPOTS_COVER_IMAGES_URL + scenicCommentImg.getImgName());
					}
					info.setScenicSpotsImgs(scenicSpotsImgsArray);
				}
				
				GenerateLog.outDebug("****************************结果数据为："+ scenicSpotsInfos.toString() +"**********************");
				JsonParser jsonParser = new JsonParser();
				JsonArray jsonArray = jsonParser.parse(gson.toJson(scenicSpotsInfos)).getAsJsonArray();
				GenerateLog.outDebug("****************************结果数据为："+ jsonArray +"**********************");
				JsonObject tempJsonObject = acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject();
				GenerateLog.outDebug(tempJsonObject);
				// 包括查询后的结果数据
				//tempJsonObject.add(ArgsConfig.RESULT_DATA, jsonArray.toString());
				tempJsonObject.add(ArgsConfig.RESULT_DATA, jsonArray);
				GenerateLog.outDebug(tempJsonObject+"11111111111111111");
				resultJson.add(ArgsConfig.REQUEST_DATA, acceptJsonObject.get(ArgsConfig.REQUEST_DATA).getAsJsonObject());
				GenerateLog.outDebug("***************************返回的数据：" + resultJson + "**********************************");
			}
			return resultJson;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
		
	}
}
